﻿Imports System.Data.SqlClient

Public Class frmDotacion
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal
    Private mstrValor As String
    Private mstrCondicion As String
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property
    Public ReadOnly Property Valor() As String
        Get
            Valor = mstrValor
        End Get
    End Property
    Private Sub frmDotacion_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró a Modificación de Boletos")
        Usuario.Ubicacion("Modulo de Liquidación")
    End Sub

    Private Sub frmDotacion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        FormateaGrid()
        CargaBoletos()

        Bitacora.RegistraActividad("Ingresó a Modificación de Boletos")
        Usuario.Ubicacion("Modificación de Boletos")

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


    End Sub


    Private Sub FinDeEdicion(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dgDotacion.CellEndEdit
        'Añadir aqui las validaciones necesarias
        Dim Inicial As Double
        Dim Final As Double
        Dim Total As Double
        Dim cidBoleto As Integer

        Try
            cidBoleto = dgDotacion.Rows(e.RowIndex).Cells(0).Value
            Inicial = dgDotacion.Rows(e.RowIndex).Cells(4).Value
            Final = dgDotacion.Rows(e.RowIndex).Cells(5).Value
            Total = (Final - Inicial) + 1
            dgDotacion.Rows(e.RowIndex).Cells(6).Value = Total
            Call SetSqlCitra("UPDATE KARDEX SET INICIAL=" & Inicial & ", FINAL=" & Final & " WHERE IdBoleto=" & cidBoleto)
        Catch ex As Exception
            '.... Accion en caso de excepcion
        End Try
    End Sub

    Sub FormateaGrid()
        dgDotacion.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular)

        dgDotacion.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dgDotacion.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dgDotacion.Columns(4).DefaultCellStyle.Format = "N0"
        dgDotacion.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgDotacion.Columns(5).DefaultCellStyle.Format = "N0"
        dgDotacion.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgDotacion.Columns(6).DefaultCellStyle.Format = "N0"
        dgDotacion.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

        dgDotacion.Columns(2).ReadOnly = True
        dgDotacion.Columns(3).ReadOnly = True
        dgDotacion.Columns(6).ReadOnly = True

    End Sub
    Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Close()

    End Sub
    Sub CargaBoletos()
        dgDotacion.Rows.Clear()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor

        Dim cnconn As New SqlConnection
        Dim strSql As String = ""
        Dim cmdBuscar As New SqlCommand
        Dim cmdAsignar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnconn.ConnectionString = CitraConnection
            cnconn.Open()

            cmdBuscar.Connection = cnconn
            cmdAsignar.Connection = cnconn

            strSql = "SELECT Kardex.IdBoleto,Kardex.IdAutobus,Autobuses.NoEconomico,Kardex.Serie,Kardex.Inicial,Kardex.Final " & _
                   "FROM Kardex INNER JOIN Autobuses " & _
                   "ON Kardex.IdAutobus=Autobuses.IdAutobus " & _
                   "WHERE Final-Inicial>1 " & _
                   "ORDER BY NoEconomico,Serie"

            cmdBuscar.CommandText = strSql
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                dgDotacion.Rows.Add(rdBuscar("idboleto"), rdBuscar("IdAutobus"), rdBuscar("NoEconomico").ToString.Trim, rdBuscar("Serie").ToString.Trim, rdBuscar("inicial"), rdBuscar("final"), CDbl(rdBuscar("inicial")) - CDbl(rdBuscar("inicial")) + 1)
                Application.DoEvents()
            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try
       
       

    End Sub

    Private Sub txtBuscar_TextChanged(sender As System.Object, e As System.EventArgs)
        CargaBoletos()
    End Sub

    Private Sub txtBuscar_TextChanged_1(sender As System.Object, e As System.EventArgs)
        CargaBoletos()

    End Sub
End Class